//-----------------------------------------------------------------------
// <copyright file="DeliveredOrderDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class DeliveredOrderDAOBase
    {
        #region Common methods
        public virtual DeliveredOrder CreateDeliveredOrderFromReader(IDataReader reader)
        {
            DeliveredOrder item = new DeliveredOrder();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("DeliveredOrderID"))) item.DeliveredOrderID = (int)reader["DeliveredOrderID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OfficeID"))) item.OfficeID = (int)reader["OfficeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SupplierCode"))) item.SupplierCode = (string)reader["SupplierCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("OrderNo"))) item.OrderNo = (string)reader["OrderNo"];
                if (!reader.IsDBNull(reader.GetOrdinal("YY"))) item.YY = (string)reader["YY"];
                if (!reader.IsDBNull(reader.GetOrdinal("OrderDate"))) item.OrderDate = (DateTime)reader["OrderDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("QtyIssued"))) item.QtyIssued = (int)reader["QtyIssued"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateIssued"))) item.DateIssued = (DateTime)reader["DateIssued"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductCode"))) item.ProductCode = (string)reader["ProductCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("BulkOrderID"))) item.BulkOrderID = (int)reader["BulkOrderID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CustomerName"))) item.CustomerName = (string)reader["CustomerName"];
                if (!reader.IsDBNull(reader.GetOrdinal("POID"))) item.POID = (int)reader["POID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateDeliveredOrderFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateDeliveredOrder methods
            
        public virtual void CreateDeliveredOrder(DeliveredOrder deliveredOrder)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersInsert");
                
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, deliveredOrder.OfficeID);
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, deliveredOrder.SupplierCode);
                dbCommandWrapper.AddInParameter("@OrderNo", DbType.AnsiString, deliveredOrder.OrderNo);
                dbCommandWrapper.AddInParameter("@YY", DbType.AnsiStringFixedLength, deliveredOrder.YY);
                dbCommandWrapper.AddInParameter("@OrderDate", DbType.DateTime, deliveredOrder.OrderDate);
                dbCommandWrapper.AddInParameter("@QtyIssued", DbType.Int32, deliveredOrder.QtyIssued);
                dbCommandWrapper.AddInParameter("@DateIssued", DbType.DateTime, deliveredOrder.DateIssued);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, deliveredOrder.ProductCode);
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, deliveredOrder.BulkOrderID);
                dbCommandWrapper.AddInParameter("@CustomerName", DbType.AnsiString, deliveredOrder.CustomerName);
                dbCommandWrapper.AddInParameter("@POID", DbType.Int32, deliveredOrder.POID);
                dbCommandWrapper.AddOutParameter("@DeliveredOrderID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                deliveredOrder.DeliveredOrderID = (int)dbCommandWrapper.GetParameterValue("@DeliveredOrderID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateDeliveredOrderException, ex);
            }
        }

        #endregion

        #region UpdateDeliveredOrder methods
        
        public virtual void UpdateDeliveredOrder(DeliveredOrder deliveredOrder)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersUpdate");            
                
                dbCommandWrapper.AddInParameter("@DeliveredOrderID", DbType.Int32, deliveredOrder.DeliveredOrderID);
                dbCommandWrapper.AddInParameter("@OfficeID", DbType.Int32, deliveredOrder.OfficeID);
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, deliveredOrder.SupplierCode);
                dbCommandWrapper.AddInParameter("@OrderNo", DbType.AnsiString, deliveredOrder.OrderNo);
                dbCommandWrapper.AddInParameter("@YY", DbType.AnsiStringFixedLength, deliveredOrder.YY);
                dbCommandWrapper.AddInParameter("@OrderDate", DbType.DateTime, deliveredOrder.OrderDate);
                dbCommandWrapper.AddInParameter("@QtyIssued", DbType.Int32, deliveredOrder.QtyIssued);
                dbCommandWrapper.AddInParameter("@DateIssued", DbType.DateTime, deliveredOrder.DateIssued);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, deliveredOrder.ProductCode);
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, deliveredOrder.BulkOrderID);
                dbCommandWrapper.AddInParameter("@CustomerName", DbType.AnsiString, deliveredOrder.CustomerName);
                dbCommandWrapper.AddInParameter("@POID", DbType.Int32, deliveredOrder.POID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateDeliveredOrderException, ex);
            }
        }
        
        #endregion

        #region DeleteDeliveredOrder methods
        public virtual void DeleteDeliveredOrder(int deliveredOrderID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersDelete");
                
                dbCommandWrapper.AddInParameter("@DeliveredOrderID", DbType.Int32, deliveredOrderID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteDeliveredOrderException, ex);
            }
        }

        #endregion

        #region GetDeliveredOrder methods
        
        public virtual DeliveredOrder GetDeliveredOrder(int deliveredOrderID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersGet");
                
                dbCommandWrapper.AddInParameter("@DeliveredOrderID", DbType.Int32, deliveredOrderID);
                
                DeliveredOrder deliveredOrder =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        deliveredOrder = CreateDeliveredOrderFromReader(reader);
                        reader.Close();
                    }
                }
                return deliveredOrder;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetDeliveredOrderException, ex);
            }
        }
        
        #endregion

        #region GetDeliveredOrders methods
        public virtual DeliveredOrderCollection GetDeliveredOrders(DeliveredOrderColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spDeliveredOrdersGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                DeliveredOrderCollection deliveredOrderCollection = new DeliveredOrderCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        DeliveredOrder deliveredOrder = CreateDeliveredOrderFromReader(reader);
                        deliveredOrderCollection.Add(deliveredOrder);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return deliveredOrderCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetDeliveredOrdersException, ex);
            }
        }
        
        public virtual DeliveredOrderCollection GetDeliveredOrders(DeliveredOrderColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetDeliveredOrders(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}